package com.xhwl.logistics.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: platform
 * @description: 字符串转成时间格式
 * @author: lu.Aaron
 * @create: 2020-07-11 13:09
 **/
public class DateFormatUtil {
    /**
     * 解析时间字符串 类似 20170404171636 / 20170404 这两种特殊格式
     * @param dateString 传入一个时间段字符串
     * @return 格式正确则返回对应的java.util.Date对象 格式错误返回null
     */
    public static Date stringDate2Date(String dateString) {
        // 科学计数法数据转为字符串
        try {
            int len = dateString.length();
            if (len == 8) {
                // dateString = 20170404
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                return sdf.parse(dateString);
            } else if (len == 17) {
                // dateString = 20170404171636
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
                return sdf.parse(dateString);
            }else if (len == 14) {
                // dateString = 20170404171636
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm");
                return sdf.parse(dateString);
            }else if (len == 19) {
                // dateString = 20170404171636
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return sdf.parse(dateString);
            }else if (len == 10) {
                // dateString = 20170404171636
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.parse(dateString);
            }
        } catch (Exception e) {
        }
        return null;
    }

    public static Date stringDate2Date(String dateString,String format) {
        // 科学计数法数据转为字符串
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String date2Str(Date date, String format) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (date != null) {
            result = sdf.format(date);
        }
        return result;
    }

    /**
     * 时间区间计算
     * @param currentTime 当前时间
     * @param createTime 数据创建时间
     * @return hour-minute 小时-分钟(字符串)
     */
    public static String limitTimeCalculator(String currentTime, String createTime){
        try{
            long nh = 1000L * 60 * 60;
            long nm = 1000L * 60;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d1 = df.parse(currentTime);
            Date d2 = df.parse(createTime);
            long diff = d1.getTime() - d2.getTime();
            long hour = diff/nh;
            long min = diff/nm;
            String diffTime = hour + "-" + min;
            return diffTime;

        }catch(Exception e){
            e.printStackTrace();
        }
        return "ERROR";
    }

    /**
     * 时间格式转换
     * @param inputTime yyyy-MM-dd HH:mm:ss
     * @return outputTime yyyy/MM/dd HH:mm:ss
     */
    public static String timeFormatTranform(String inputTime){
        try{
            SimpleDateFormat inputSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date inputTimeDate = inputSdf.parse(inputTime);
            SimpleDateFormat outputTimeSdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String outputTime = outputTimeSdf.format(inputTimeDate);
            return outputTime;

        }catch(Exception e){
            e.printStackTrace();
        }
        return "ERROR";
    }

    //获取时间差方法
    public static String getTime(Date currentTime,Date firstTime){
        long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
        long hours = (diff)/(1000* 60 * 60); //获取时
        long minutes = (diff-hours*(1000* 60 * 60))/(1000* 60);  //获取分钟
        long s=(diff/1000-hours*60*60-minutes*60);//获取秒
        String CountTime=""+hours+"小时"+minutes+"分"+s+"秒";
        return CountTime;
    }
    //Date类型转Calendar类型
    public static Calendar dataToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }


    /**
     * 获取精确到秒的时间戳
     * @return
     */
    public static String getSecondTimestamp(Date date){
        if (null == date) {
            return "0";
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return timestamp.substring(0,length-3);
        } else {
            return "0";
        }
    }


    /**
     * 获取指定日期所在月份开始的时间 第一天
     * lu.aaron
     * @return
     */
    public static String getMonthBegin(String specifiedDay) {
        Date data = null;
        try {
            data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(data);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND,0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 本月第一天的时间戳转换为字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = sdf.parse(sdf.format(new Date(new Long(c.getTimeInMillis()))));
            //Date date = sdf.parse(sdf.format(new Long(s)));// 等价于
            return sdf.format(date);
        } catch (Exception e) {

        }
        return null;
    }


    /**
     * 获取指定日期所在月份结束的时间 最后一天
     * @return
     */
    public static String getMonthEnd(String specifiedDay) {
        Date data = null;
        try {
            data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(data);

        //设置为当月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        c.set(Calendar.MINUTE, 59);
        //将秒至59
        c.set(Calendar.SECOND, 59);
        //将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 本月第一天的时间戳转换为字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = sdf.parse(sdf.format(new Date(new Long(c.getTimeInMillis()))));
            //Date date = sdf.parse(sdf.format(new Long(s)));// 等价于
            return sdf.format(date);
        } catch (Exception e) {

        }
        return null;
    }
    /**
     * @author: Lu.Aaron
     * @description:
     * TODO
     * 1.一小时内显示：xxx分钟
     * 2.超过一小时显示：x小时x分钟
     * 3.超过1天显示：x天x小时xx分钟
     * 4.超过一个月以天显示：x天x小时x分钟
     * @date: 2020/8/31 10:10
     * @param startTime
     * @param endTime
     * @return java.lang.String
     */
    public static String getLimitTime(Date endTime,Date startTime){
        //这样得到的差值是微秒级别
        long diff = endTime.getTime() - startTime.getTime();
        long days = diff / (1000 * 60 * 60 * 24);
        //获取时
        long hours = (diff)/(1000* 60 * 60);
        //获取分钟
        long minutes = (diff-hours*(1000* 60 * 60))/(1000* 60);
        String CountTime=null;
        if (days>=1) {
            //获取时
            hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
            CountTime=""+days+"天"+hours+"小时"+minutes+"分钟";

        }else{
            if (hours>=1) {
                CountTime=""+hours+"小时"+minutes+"分钟";
            }else {
                CountTime=""+minutes+"分钟";
            }
        }
        return CountTime;
    }

    /**
     * 获取两个时间相隔的天数
     * @param start
     * @param end
     * @return
     * @throws ParseException
     */
    public static int getDiffDay(String start,String end) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = simpleDateFormat.parse(start);
        //fix bug：当新增车辆时，绑定的卡类型为月卡和免费卡时，end的值为""（空字符串），end无法解析为时间endTime
        Date endTime;
        if ("".equals(end)) {
            endTime = startTime;
        } else {
            endTime = simpleDateFormat.parse(end);
        }
        //fix bug：当新增车辆时，绑定的卡类型为月卡和免费卡时，end的值为""（空字符串），end无法解析为时间endTime
        return (int)Math.ceil((endTime.getTime()-startTime.getTime())/(24*60*60*1000d));
    }

    /**
     *
     * @param diff 传的是秒
     * @return
     */
    public static String getTime(long diff){
        long days = diff / (60 * 60 * 24);
        long hours = (diff % (60 * 60 * 24)) / (60 * 60);
        long minutes = (diff % (60 * 60)) / 60;
        long s = diff % 60;

        String CountTime=null;
        if (days > 0) {
            CountTime = days + "天" + hours + "小时" + minutes + "分钟" + s + "秒";
        } else if (hours > 0) {
            CountTime = hours + "小时" + minutes + "分钟" + s + "秒";
        } else if (minutes > 0) {
            CountTime = minutes + "分钟" + s + "秒";
        } else {
            CountTime = s + "秒";
        }

        return CountTime;

    }

    public static void main(String[] args) {
        int a=194421*60;
        String time = getTime(a);
        System.out.println("time = " + time);
    }
}

